// 04myCenter/02set/01bindPhone/bindPhone.js
var co = require('../../../resource/js/common.js')
var app = getApp()
var _this, _data
var _inviter_User_ID = 0
//首页输入的手机，
var _inputPhone
//验证码
var _sms_Code
//联盟id
var _unionid
//openid
var _openid
//参数
var _opt = {}
//是否关闭倒计时
var isCloseCountDown = false
//参数信息
var paramInfo = {}
//表单ID
var _form_id
Page({

  /**
   * 页面的初始数据
   */
  data: {
     getSmsCodeText:'',
     //验证是否ok
     verified_Ok: 0,
     passAuth:false,
     userInfo:'',//用户信息
     showBindPhone:true,//显示已绑定手机
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     _this = this
     _data = this.data
     var userInfo = app.Data.userInfo
     console.log('userInfo',userInfo)
     _unionid = wx.getStorageSync('unionid')
     //console.log('_unionid', _unionid)
     _this.setData({
        userInfo: userInfo,
        verified_Ok: 0,
        getSmsCodeText: '获取验证码'
     })

  },
  //更换绑定手机
   replacePhone:function(){
      this.setData({
         showBindPhone:false,
      })

   },
   //检查手机格式
   checkPhone: function (phone) {
      var phoneNumber = phone ? phone : _inputPhone
      var rule = /^1[0-9]{10}$/
      //console.log('【checkPhone(phone)==】' + phoneNumber)
      return rule.test(phoneNumber) ? 1 : 0
   },

   //一键获取微信认证手机
   getPhoneNumber: function (e) {
      app.decryptWxInfoData(e.detail, function (res) {
         console.log(res)
         _this.setData({
            verified_Ok: 1,
            verified_Phone: res.phoneNumber,
            passAuth:true
         })
      })
   },

   //输入手机
   inputPhone: function (e) {
      _inputPhone = e.detail.value
      //console.log('【inputPhone】===' + _inputPhone)
   },

   //换个手机
   changePhone: function (e) {
      _this.setData({
         verified_Ok: 0,
         verified_Phone: '',
      })
   },
   //提示手机认证
   showToast: function (e) {
      co.toastAllText('请先手机认证')
   },

   //输入验证码
   inputCode: function (e) {
      console.log('输入码==' + e.detail.value)
      var input_sms_Code = e.detail.value
      //判断验证码
      if (input_sms_Code.length > 6 ||
         (input_sms_Code.length == 6 && input_sms_Code != _sms_Code)) {
         co.toastAllText('验证码不正确')
      } else if (input_sms_Code == _sms_Code) {
         isCloseCountDown = true
         _this.setData({
            verified_Ok: 1,
            verified_Phone: _data.verified_Phone,
            passAuth:true
         })
      }
   },

   //获取短信验证码
   getPhoneCode: function (e) {
      //判断手机格式
      if (!_this.checkPhone(_inputPhone)) {
         co.toastAllText('手机号格式不正确2')
         return
      }
      _data.verified_Phone = _inputPhone
      //获取验证码
      co.getShareApi('api/Sms/getPhoneCode', {
         mobile: _data.verified_Phone,
         type: 'user_bind',
         appid: 1400134010
      }, function (res) {
         if (res.code == 0) {
            _sms_Code = res.data.Sms_Code
            var second = 90
            _this.setData({
               getSmsCodeText: '重新获取(' + second + '秒)'
            })
            _this.countDown(second)
         } else {
            co.toastAllText(res.msg)
         }
      })
   },

   //倒计时
   countDown: function (time) {
      setTimeout(function () {
         if (!isCloseCountDown && time > 1) {
            //显示倒计时
            _this.setData({
               getSmsCodeText: '重新获取(' + time + '秒)'
            })
            time--
            //递归
            _this.countDown(time)
         } else {
            isCloseCountDown = false
            _this.setData({
               getSmsCodeText: '重新获取'
            })
         }
      }, 1000);
   },

   //绑定手机
   bindPhone:function(){
      var url = 'jyhs/Usermg/updateInfo'     
      co.getShareApi(url, {
         User_ID: app.Data.userInfo.User_ID,
         User_Mobile: _inputPhone
      }, function (res) {
         if (res.code == 0) {
            co.toastAllText(res.msg)
            app.Data.userInfo = res.data
            setTimeout(function () {
               var pages = getCurrentPages(),
                  prevPage = pages[pages.length - 2];
               prevPage.setData({
                  alter: "123"
               })
               wx.navigateBack({})
            }, 1000)
         } else {
            co.toastAllText(res.msg)
         }

      })
   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})